#
#    Copyright 2007 Andrew Wilkinson <andrew@indiegigs.co.uk>.
#
#    This file is part of Transport Manager (http://code.google.com/p/transportmanager/)
#
#    Transport Manager is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2.0 of the License, or
#    (at your option) any later version.
#
#    Transport Manager is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Transport Manager; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

from PyQt4 import QtGui

from grass import Grass
from straight_track import StraightTrack
from track_manager import TrackManager

class World(QtGui.QGraphicsItem):
    def __init__(self):
        QtGui.QGraphicsItem.__init__(self)
        self.width, self.height = 10, 10

        self.track_manager = TrackManager()

    def init(self):
        self.scale(32, 32)
        self.setZValue(-1.0)
        screen.scene.addItem(self)
        self.tiles = [[Grass(x, y) for y in range(self.height)] for x in range(self.width)]

    def update(self, delay):
        map(lambda row: map(lambda x: x.update(delay), row), self.tiles)

    def onClick(self, tile):
        if tile.lowest.above is None:
            self.track_manager.add(tile.x, tile.y)

    def __getitem__(self,  (x, y)):
        assert x >= 0 and x < self.width
        assert y >= 0 and y < self.height
        return self.tiles[x][y]
